/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 *
 * @author yaoyuming
 *
 * @date 2019-11-29 16:02
 */
package com.yao.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.yao.constant.Const;
import com.yao.core.Table;

import java.io.File;
import java.util.List;

/**
 * 创建sql工具类
 *
 * @author yaoyuming
 * @date 2019/11/29 16:02
 */
public class SqlUtil {

    public static void createSql(String ftl, String outPath, List<Table> tables) {

        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig(Const.FTL_DIR, TemplateConfig.ResourceMode.CLASSPATH));
        Template template = engine.getTemplate(ftl);
        File file = FileUtil.newFile(outPath);
        FileUtil.del(file);
        tables.stream().forEach(table -> {
            // 每个table生成一个完整的创建表结构语句
            String render = template.render(Dict.create().parseBean(table));
            FileUtil.appendUtf8String(StrUtil.concat(true, render, "\n\n"), file);
        });


    }
}